package dj;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AudycjaAdd extends HttpServlet  {
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("audycja.jsp").forward(req, resp);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String driver = "org.h2.Driver";
	try {Class.forName(driver).newInstance();	} catch (InstantiationException e1) {		e1.printStackTrace();	} catch (IllegalAccessException e1) {				e1.printStackTrace();	} catch (ClassNotFoundException e1) {				e1.printStackTrace();	}
	Connection con=null;
	ResultSet rst=null;
	Statement stmt=null;
	try{		con=DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
		stmt=con.createStatement();
	}
	catch(Exception e){
		System.out.println(e.getMessage());
	}

		String nazwa=request.getParameter("nazwa");
		String opis=request.getParameter("opis");
		String godzina=request.getParameter("godzina");
		String dj=request.getParameter("dj");
		String poniedzialek;if(request.getParameter("poniedzialek")==null){poniedzialek="n";}else{poniedzialek=request.getParameter("poniedzialek");}
		String wtorek;if(request.getParameter("wtorek")==null){wtorek="n";}else{wtorek=request.getParameter("wtorek");}
		String sroda;if(request.getParameter("sroda")==null){sroda="n";}else{sroda=request.getParameter("sroda");}
		String czwartek;if(request.getParameter("czwartek")==null){czwartek="n";}else{czwartek=request.getParameter("czwartek");}
		String piatek;if(request.getParameter("piatek")==null){piatek="n";}else{piatek=request.getParameter("piatek");}
		String sobota;if(request.getParameter("sobota")==null){sobota="n";}else{sobota=request.getParameter("sobota");}
		String niedziela;if(request.getParameter("niedziela")==null){niedziela="n";}else{niedziela=request.getParameter("niedziela");}
	// create table audycje(id int AUTO_INCREMENT, nazwa varchar,opis varchar,godzina varchar,poniedzialek varchar,wtorek varchar,sroda varchar,czwartek varchar,piatek varchar,sobota varchar,niedziela varchar)	
		try {//insert into dj(imie,nazwisko,pseudonim,email,gatunek) values('"+imie+"','"+nazwisko+"','"+pseudonim+"','"+email+"','"+gatunek+"')"); 
			stmt.executeUpdate("insert into audycje(nazwa,opis,godzina,poniedzialek,wtorek,sroda,czwartek,piatek,sobota,niedziela,dj) values('"+nazwa+"','"+opis+"','"+godzina+"','"+poniedzialek+"','"+wtorek+"','"+sroda+"','"+czwartek+"','"+piatek+"','"+sobota+"','"+niedziela+"','"+dj+"')");
		} catch (SQLException e) {e.printStackTrace();		}
		try {
			rst=stmt.executeQuery("select * from audycje");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();

		writer.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'	'http://www.w3.org/TR/html4/strict.dtd'>");
		writer.write("<h2> Wprowadzono </h2>");
		writer.write("<br/>");
		writer.write("<table>");
		startTableRow(writer);
		addTableColumn(writer, "Nazwa");
		addTableColumn(writer, nazwa);
		closeTableRow(writer);
		startTableRow(writer);
		addTableColumn(writer, "Opis");
		addTableColumn(writer, opis);
		closeTableRow(writer);
		startTableRow(writer);
		addTableColumn(writer, "Godzina");
		addTableColumn(writer, godzina);
		closeTableRow(writer);
		writer.write("</table>");
		writer.write("<a href=index.jsp>Powrot</a>");
		writer.close(); 
	}
	private static void startTableRow(PrintWriter writer) {
	writer.write("<tr>");	
	}
	private static void closeTableRow(PrintWriter writer) {
		writer.write("</tr>");	
	}
	private static void addTableColumn(PrintWriter writer, String value) {
		writer.write("<td> "+ value + "</td>");	
	}
}